package com.manli.api.service;

import com.manli.api.pojo.QueryParamsActivityGoods;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.ActivityGoodsDao;
import com.manli.api.dto.ActivityGoods;


@Service("activityGoodsService")
public class ActivityGoodsService {
    @Autowired
    ActivityGoodsDao activityGoodsDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParamsActivityGoods params) {
        StringBuffer sql = new StringBuffer("SELECT " +
                " a.*, " +
                " if(a.`status` = '1','启用','禁用') activity_goods_status, " +
                " b.activity_type_name, " +
                " if(b.`status` = '1','启用','禁用') activity_type_status, " +
                " c.goods_name, " +
                " d.branch_name, " +
                " e.partner_name  " +
                "FROM " +
                " activity_goods a " +
                " LEFT JOIN activity_type b ON a.activity_type_id = b.activity_type_id " +
                " LEFT JOIN goods_def c ON a.goods_def_id = c.goods_def_id " +
                " LEFT JOIN branch d ON c.branch_id = d.branch_id " +
                " LEFT JOIN partner e ON e.partner_id = c.partner_id where 1=1 ");
        sql.append(params.getStatus() == null?"":" and a.status = " +params.getStatus());
        sql.append(params.getGoodsDefId() == null?"":" and a.goods_def_id = " +params.getGoodsDefId());
        sql.append(params.getActivityTypeId() == null?"":" and a.activity_type_id = " +params.getActivityTypeId());
        sql.append(params.getBranchId() == null?"":" and d.branch_id = " +params.getBranchId());
        sql.append(params.getPartnerId() == null?"":" and e.partner_id = " +params.getPartnerId());

        String activityStartEndTimeSql =
                " and (" +
                        (StringUtils.isEmpty(params.getActivityStartTime()) ?"":" (a.activity_start_time <= '" +params.getActivityStartTime() + "' and a.activity_end_time >= ' "  +params.getActivityStartTime() + "' )" )+
                        " or " +
                        (StringUtils.isEmpty(params.getActivityEndTime())?"":" (a.activity_start_time <= '" +params.getActivityEndTime()   + "' and a.activity_end_time >= '"   +params.getActivityEndTime() + "' )") +
                      ")";
        activityStartEndTimeSql = StringUtils.isEmpty(params.getActivityStartTime()) ?"":activityStartEndTimeSql;
        activityStartEndTimeSql = StringUtils.isEmpty(params.getActivityEndTime()) ?"":activityStartEndTimeSql;
        sql.append(activityStartEndTimeSql);

        sql.append(StringUtils.isEmpty(params.getGoodsName())?"":" and c.goods_name like '%" +params.getActivityEndTime() + "%'");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<ActivityGoods> pageQuery = new PageQuery<ActivityGoods>(params.getPage(),params.getLimit());
        PageQuery<ActivityGoods> query =sqlManager.execute(sqlReady,ActivityGoods.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        StringBuffer sql = new StringBuffer("SELECT " +
                " a.*, " +
                " if(a.`status` = 1,'启用','禁用') activity_goods_status, " +
                " b.activity_type_name, " +
                " if(b.`status` = 1,'启用','禁用') activity_type_status, " +
                " c.goods_name, " +
                " d.branch_name, " +
                " e.partner_name  " +
                "FROM " +
                " activity_goods a " +
                " LEFT JOIN activity_type b ON a.activity_type_id = b.activity_type_id " +
                " LEFT JOIN goods_def c ON a.goods_def_id = c.goods_def_id " +
                " LEFT JOIN branch d ON c.branch_id = d.branch_id " +
                " LEFT JOIN partner e ON e.partner_id = c.partner_id where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());
        List<ActivityGoods> list =sqlManager.execute(sqlReady,ActivityGoods.class);
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer activityGoodsId) {
        ActivityGoods activityGoods = activityGoodsDao.single(activityGoodsId);
        return ResultUtil.success(activityGoods);
    }

    @Transactional
    public Result save(ActivityGoods activityGoods) {
        if(activityGoods.getStatus() == null){
            return ResultUtil.error(-1,"状态为空");
        }
        if(activityGoods.getActivityTypeId() == null){
            return ResultUtil.error(-1,"活动类型为空");
        }
        if(activityGoods.getGoodsDefId() == null){
            return ResultUtil.error(-1,"商品id为空");
        }
        activityGoodsDao.insert(activityGoods);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(ActivityGoods activityGoods) {
        if(activityGoods.getStatus() == null){
            return ResultUtil.error(-1,"状态为空");
        }
        if(activityGoods.getActivityTypeId() == null){
            return ResultUtil.error(-1,"活动类型为空");
        }
        if(activityGoods.getGoodsDefId() == null){
            return ResultUtil.error(-1,"商品id为空");
        }
        if(activityGoods.getActivityGoodsId() == null){
            return ResultUtil.error(-1,"id为空");
        }
        activityGoodsDao.updateTemplateById(activityGoods);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(ActivityGoods activityGoods) {
        activityGoodsDao.updateTemplateById(activityGoods);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            activityGoodsDao.deleteById(id);
        }
        return ResultUtil.success();
    }



}
